//
//  RobVC.m
//  RPay
//
//  Created by hzy on 2017/11/1.
//  Copyright © 2017年 Charlie. All rights reserved.
//

#import "RobVC.h"
#import "TMNetwork.h"
#import "RecordModel.h"
#import "RedPacketRecordCell.h"
#import "OpenAnimationView.h"
#import "RefreshManager.h"
#import "OpenRedPacketVC.h"

@interface RobVC ()<UITableViewDataSource,UITableViewDelegate,CAAnimationDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray * dataSource;
@property (nonatomic,strong)OpenAnimationView * animationView;

@end

@implementation RobVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"抢红包";
    [self setupUI];

    [self addNavigationBarLeftButtonItem];

    
    // Do any additional setup after loading the view from its nib.
}

-(void)setupUI{
    _dataSource = [NSMutableArray array];


    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    
    
}
-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    [self getRednetwork];
}

-(void)getRednetwork{
    //1509455609230
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[TMNetwork sharedInstance] GET:@"/open/appRedPacket/getRedPacket.shtml?" headerParams:nil queryParams:nil formParams:@{@"userName":[ShareUser shareUser].userName,@"type":@"unTakeIn",@"pageIndex":@"1",@"pageSize":@"-1"} withToken:NO model:nil progress:^(NSProgress *progress) {
        
    } success:^(id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        
        if ([self checkNetworkData:responseObject]) {
            if (responseObject[@"content"]) {
                _dataSource = [RecordModel mj_objectArrayWithKeyValuesArray:responseObject[@"content"][@"page"]];
                

                [_tableView reloadData];
            }
            
            
        }
        
    } failure:^(id responseObject) {
        [self alertNetworkError];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  _dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 55;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static  NSString  *CellIdentiferId = @"RedPacketRecordCell";
    RedPacketRecordCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"RedPacketRecordCell" owner:nil options:nil];
        cell = [nibs lastObject];
        cell.backgroundColor = [UIColor clearColor];
    };
    RecordModel * model = _dataSource[indexPath.row];
    NSString * string = nil;

    
    string =[NSString stringWithFormat:@"来自 %@ 的红包",model.sendUser];

        cell.fromLab.text = string;
    cell.moneyLab.hidden = YES;
    cell.timeLab.text = model.sendTime;
    
    return cell;
}
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MJWeakSelf
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    RecordModel * model = _dataSource[indexPath.row];
    [self showAnimationView:model];
    
    
    
}
-(void)showAnimationView:(RecordModel *)model{

    MJWeakSelf
    _animationView = [[[NSBundle mainBundle] loadNibNamed:@"OpenAnimationView" owner:nil options:nil]lastObject];
    _animationView.frame = CGRectMake(0, 0, DEVICE_WITH, DEVICE_HEIGHT);
    
    _animationView.openBlock = ^{
      
        
        [weakSelf openRednetwork:model];

        
    };
    
    [[[[UIApplication sharedApplication]delegate]window]addSubview:_animationView];
    

    
}







-(void)openRednetwork:(RecordModel *)model{
    
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[TMNetwork sharedInstance] POSTWithJSONRequestSerialized:[NSString stringWithFormat:@"%@id=%@&userNumber=%@",@"/open/appRedPacket/takeInRedPacket.shtml?",model.aid,[ShareUser shareUser].userName] headerParams:nil queryParams:nil formParams:@{} withToken:NO model:nil progress:^(NSProgress *progress) {
        
    } success:^(id responseObject) {
        
        
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        
        if ([self checkNetworkData:responseObject]) {
            
            
            OpenRedPacketVC * vc = [[OpenRedPacketVC alloc] initWithNibName:@"OpenRedPacketVC" bundle:[NSBundle mainBundle]];
            vc.aid = model.aid;
            vc.isMine = YES;
            [self.navigationController pushViewController:vc animated:YES];
            
            
        }
        [_animationView removeFromSuperview];
        
    } failure:^(id responseObject) {
        [_animationView removeFromSuperview];
        [self alertNetworkError];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
}

@end
